﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Theme_Entity : EntityBase<THEME>
    {
        public override RESP_Info Add(THEME model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into THEME(THEME_ID,THEME_NAME,THEME_DES) values (@THEME_ID,@THEME_NAME,@THEME_DES)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("THEME_ID", model.THEME_ID));
                    cmd.Parameters.Add(new SqlParameter("THEME_NAME", model.THEME_NAME));
                    cmd.Parameters.Add(new SqlParameter("THEME_DES", model.THEME_DES));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加主题成功</p></h5>";
                    info.Url = "ThemeList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加主题失败</p></h5>";
                    info.Url = "ThemeAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加主题失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(THEME model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update THEME set THEME_NAME=@THEME_NAME,THEME_DES=@THEME_DES where THEME_ID=@THEME_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("THEME_ID", model.THEME_ID));
                    cmd.Parameters.Add(new SqlParameter("THEME_NAME", model.THEME_NAME));
                    cmd.Parameters.Add(new SqlParameter("THEME_DES", model.THEME_DES));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑主题成功</p></h5>";
                    info.Url = "ThemeList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑主题失败</p></h5>";
                    info.Url = "ThemeAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑主题失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(THEME theme)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from THEME where THEME_ID=@THEME_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("THEME_ID", theme.THEME_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除主题成功</p></h5>";
                    info.Url = "ThemeList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除主题失败</p></h5>";
                    info.Url = "ThemeList.aspx";

                    Tom.Net.Log.LogManager.Error("删除主题失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override THEME GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select THEME_ID,THEME_NAME,THEME_DES from THEME where THEME_ID=@THEME_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("THEME_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        THEME theme = new THEME();
                        if (!reader.IsDBNull(0)) theme.THEME_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) theme.THEME_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) theme.THEME_DES = reader.GetString(2);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return theme;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取主题数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override THEME GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        THEME theme = new THEME();
                        if (!reader.IsDBNull(0)) theme.THEME_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) theme.THEME_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) theme.THEME_DES = reader.GetString(2);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return theme;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取主题数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<THEME> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<THEME> themes = new List<THEME>();
                    while (reader.Read())
                    {
                        THEME theme = new THEME();
                        if (!reader.IsDBNull(0)) theme.THEME_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) theme.THEME_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) theme.THEME_DES = reader.GetString(2);

                        themes.Add(theme);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return themes;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取主题数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
